package boogui.admin;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import boogui.member.S_UISetting;

public class S_ShowingRegistryDialog extends JDialog {

	private ShowingRegistryService srs = null;
	
	private static S_ShowingRegistryDialog dialog;
	private RegiActionListener al = null;
	private RegiKeyListener kl = null;

	private JPanel contentPane;
	private JComboBox<String> titleCbx;
	private JComboBox<String> locationCbx;
	private JComboBox<String> cinemaCbx;
	private JComboBox<String> theaterCbx;
	private JComboBox<String> yearCbx;
	private JComboBox<String> monthCbx;
	private JComboBox<String> dayCbx;
	private JComboBox<String> hourCbx;
	private JComboBox<String> minCbx;
	
	private DefaultTableModel model;

	/**
	 * Launch the application.
	 *//*
	public static void main(String[] args) {
		try {
			dialog = new S_ShowingRegistryDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
*/
	/**
	 * Create the dialog.
	 */
	public S_ShowingRegistryDialog() {
		setModal(true);
		srs = new ShowingRegistryService();
		al = new RegiActionListener();
		kl = new RegiKeyListener();
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/menu/showing.png")));
		S_UISetting.setUIFont(new FontUIResource("굴림", Font.PLAIN, 12));
		setTitle("상영 정보 등록");
		setBounds(100, 100, 800, 450);
		setResizable(false);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("등 록");
				okButton.addActionListener(al);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("취 소");
				cancelButton.addActionListener(al);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
			
			JPanel adInfoP = new JPanel();
			adInfoP.setPreferredSize(new Dimension(10, 445));
			contentPane.add(adInfoP, BorderLayout.CENTER);
			GridBagLayout gbl_adInfoP = new GridBagLayout();
			gbl_adInfoP.columnWidths = new int[]{15, 0, 5, 0, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 15, 0};
			gbl_adInfoP.rowHeights = new int[] {15, 0, 0, 5, 0, 0, 5, 0, 0, 0};
			gbl_adInfoP.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
			gbl_adInfoP.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
			adInfoP.setLayout(gbl_adInfoP);				
			
			JLabel lblNewLabel_2 = new JLabel("영 화 제 목");
			lblNewLabel_2.setFont(new Font("맑은 고딕", Font.BOLD, 13));
			GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
			gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_2.gridx = 1;
			gbc_lblNewLabel_2.gridy = 1;
			adInfoP.add(lblNewLabel_2, gbc_lblNewLabel_2);
			
			titleCbx = new JComboBox();
			srs.readMovieTitle(titleCbx);
			GridBagConstraints gbc_titleCbx = new GridBagConstraints();
			gbc_titleCbx.gridwidth = 9;
			gbc_titleCbx.insets = new Insets(0, 0, 5, 5);
			gbc_titleCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_titleCbx.gridx = 3;
			gbc_titleCbx.gridy = 1;
			adInfoP.add(titleCbx, gbc_titleCbx);
			
			JLabel lblNewLabel = new JLabel("상   영   관");
			lblNewLabel.setFont(new Font("맑은 고딕", Font.BOLD, 13));
			GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
			gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel.gridx = 1;
			gbc_lblNewLabel.gridy = 3;
			adInfoP.add(lblNewLabel, gbc_lblNewLabel);
			
			
			locationCbx = new JComboBox();		
			locationCbx.addActionListener(al);
			locationCbx.setActionCommand("location");			
			GridBagConstraints gbc_locationCbx = new GridBagConstraints();
			gbc_locationCbx.gridwidth = 3;
			gbc_locationCbx.insets = new Insets(0, 0, 5, 5);
			gbc_locationCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_locationCbx.gridx = 3;
			gbc_locationCbx.gridy = 3;
			adInfoP.add(locationCbx, gbc_locationCbx);
			srs.locationCombo(locationCbx);	
			
			cinemaCbx = new JComboBox();
			cinemaCbx.addActionListener(al);
			cinemaCbx.setActionCommand("cinema");
			GridBagConstraints gbc_cinemaCbx = new GridBagConstraints();
			gbc_cinemaCbx.fill = GridBagConstraints.HORIZONTAL;			
			gbc_cinemaCbx.gridwidth = 3;
			gbc_cinemaCbx.insets = new Insets(0, 0, 5, 5);
			gbc_cinemaCbx.gridx = 7;
			gbc_cinemaCbx.gridy = 3;
			adInfoP.add(cinemaCbx, gbc_cinemaCbx);			
			srs.cinemaCombo(cinemaCbx, (String) locationCbx.getSelectedItem());		
			
			theaterCbx = new JComboBox();
			GridBagConstraints gbc_theaterCbx = new GridBagConstraints();
			gbc_theaterCbx.insets = new Insets(0, 0, 5, 5);
			gbc_theaterCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_theaterCbx.gridx = 11;
			gbc_theaterCbx.gridy = 3;
			adInfoP.add(theaterCbx, gbc_theaterCbx);
			srs.theaterCombo(theaterCbx, (String) cinemaCbx.getSelectedItem());
			
			JLabel lblNewLabel_1 = new JLabel("상 영 시 간");
			lblNewLabel_1.setFont(new Font("맑은 고딕", Font.BOLD, 13));
			GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
			gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_1.gridx = 1;
			gbc_lblNewLabel_1.gridy = 5;
			adInfoP.add(lblNewLabel_1, gbc_lblNewLabel_1);
			
			String[] year = new String[2];
			for (int i = 0; i < year.length; ++i) {
				year[i] = (2013 + i) + "년";
			}			
			yearCbx = new JComboBox(year);
			GridBagConstraints gbc_yearCbx = new GridBagConstraints();
			gbc_yearCbx.insets = new Insets(0, 0, 5, 5);
			gbc_yearCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_yearCbx.gridx = 3;
			gbc_yearCbx.gridy = 5;
			adInfoP.add(yearCbx, gbc_yearCbx);		
			
			String[] month = new String[12];
			for (int i = 0; i < 9; ++i) {
				month[i] = "0" + (i + 1) + "월";
			}
			for (int i = 9; i < month.length; ++i) {
				month[i] = (i + 1) + "월";
			}
			monthCbx = new JComboBox(month);
			GridBagConstraints gbc_monthCbx = new GridBagConstraints();
			gbc_monthCbx.insets = new Insets(0, 0, 5, 5);
			gbc_monthCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_monthCbx.gridx = 5;
			gbc_monthCbx.gridy = 5;
			adInfoP.add(monthCbx, gbc_monthCbx);		
			
			String[] day = new String[31];
			for (int i = 0; i < 9; ++i) {
				day[i] = "0" + (i + 1) + "일";
			}
			for (int i = 9; i < day.length; ++i) {
				day[i] = (i + 1) + "일";
			}
			dayCbx = new JComboBox(day);
			GridBagConstraints gbc_dayCbx = new GridBagConstraints();
			gbc_dayCbx.insets = new Insets(0, 0, 5, 5);
			gbc_dayCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_dayCbx.gridx = 7;
			gbc_dayCbx.gridy = 5;
			adInfoP.add(dayCbx, gbc_dayCbx);
			
			String[] hour = new String[25];
			for (int i = 0; i < 9; ++i) {
				hour[i] = "0" + (i + 1) + "시";
			}
			for (int i = 9; i < hour.length; ++i) {
				hour[i] = (i + 1) + "시";
			}
			
			hourCbx = new JComboBox(hour);
			GridBagConstraints gbc_hourCbx = new GridBagConstraints();
			gbc_hourCbx.insets = new Insets(0, 0, 5, 5);
			gbc_hourCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_hourCbx.gridx = 9;
			gbc_hourCbx.gridy = 5;
			adInfoP.add(hourCbx, gbc_hourCbx);
			
			String[] min = new String[59];
			for (int i = 0; i < 10; ++i) {
				min[i] = "0" + i + "분";
			}
			for (int i = 9; i < min.length; ++i) {
				min[i] = (i+1) + "분";
			}
			
			minCbx = new JComboBox(min);
			GridBagConstraints gbc_minCbx = new GridBagConstraints();
			gbc_minCbx.insets = new Insets(0, 0, 5, 5);
			gbc_minCbx.fill = GridBagConstraints.HORIZONTAL;
			gbc_minCbx.gridx = 11;
			gbc_minCbx.gridy = 5;
			adInfoP.add(minCbx, gbc_minCbx);
			
			JButton addBtn = new JButton("추가▽");
			addBtn.addActionListener(al);
			addBtn.setActionCommand("add");
			GridBagConstraints gbc_addBtn = new GridBagConstraints();
			gbc_addBtn.anchor = GridBagConstraints.WEST;
			gbc_addBtn.insets = new Insets(0, 0, 5, 5);
			gbc_addBtn.gridx = 13;
			gbc_addBtn.gridy = 5;
			adInfoP.add(addBtn, gbc_addBtn);
			
			JScrollPane scrollPane_1 = new JScrollPane();
			scrollPane_1.setPreferredSize(new Dimension(20, 100));
			GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
			gbc_scrollPane_1.gridwidth = 13;
			gbc_scrollPane_1.insets = new Insets(0, 0, 5, 5);
			gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
			gbc_scrollPane_1.gridx = 1;
			gbc_scrollPane_1.gridy = 7;
			adInfoP.add(scrollPane_1, gbc_scrollPane_1);
			
			model = new DefaultTableModel(){
				public boolean isCellEditable(int row, int column) {
					return false;
				};
			};
			
			String title[] = {"제목", "지역", "영화관", "상영관", "상영날짜", "상영시간"};
			model.setColumnIdentifiers(title);	
			 
			JTable table = new JTable();		
			table.addKeyListener(kl);
			
			table.setModel(model);		
			DefaultTableCellRenderer cell = new DefaultTableCellRenderer();
			cell.setHorizontalAlignment(JLabel.CENTER);
			for (int i = 0; i < title.length; i++){
				table.getColumnModel().getColumn(i).setCellRenderer(cell);
			}
			//table.setRowHeight(30);
	        table.getColumn(title[0]).setPreferredWidth(150);
	        table.getColumn(title[3]).setPreferredWidth(20);
	        table.getColumn(title[5]).setPreferredWidth(80);
			scrollPane_1.setViewportView(table);
			setVisible(true);
	}
	
	private class RegiActionListener implements ActionListener {				
		public void actionPerformed(ActionEvent e) {			
			switch (e.getActionCommand()) {
			case "location":				
				srs.cinemaCombo(cinemaCbx, (String) locationCbx.getSelectedItem());
				break;
			case "cinema":	
				srs.theaterCombo(theaterCbx, (String) cinemaCbx.getSelectedItem());				
				break;			
			case "add":
				String title = (String) titleCbx.getSelectedItem();
				String location = (String) locationCbx.getSelectedItem();
				String cinema = (String) cinemaCbx.getSelectedItem();
				String theater = (String) theaterCbx.getSelectedItem();
				String date = (String) yearCbx.getSelectedItem() +" "+ (String) monthCbx.getSelectedItem() + " " + (String) dayCbx.getSelectedItem();
				String time = (String) hourCbx.getSelectedItem() + " " + (String) minCbx.getSelectedItem();				
				model.addRow(new String[] {title, location, cinema, theater, date, time});				
				break;
			case "OK":				
				if (srs.getTableModel(model)) {
					JOptionPane.showMessageDialog(null, "상영정보가 등록되었습니다.");
					setVisible(false);
					dispose();
				} else {
					JOptionPane.showMessageDialog(null, "상영정보 등록에 실패하였습니다.", "", JOptionPane.ERROR_MESSAGE);					
				}
				break;
			case "Cancel":
				setVisible(false);
				dispose();
				break;
			}
		}
	}
	
	private class RegiKeyListener implements KeyListener {
		@Override
		public void keyPressed(KeyEvent e) {
			try {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_DELETE:
					JTable table = (JTable)e.getSource();
					int row = table.getSelectedRow(); 
					model.removeRow(row);
					break;
				}
			} catch (Exception ex) {}
		}
		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
	}

}
